﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WPF_DbCenter.CommonMethod;
using CoreEA;
using CoreEA.ConnSTR;
using System.Data;
using XLCS.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Diagnostics;

namespace WPF_DbCenter.SqlCE.DbEnginer
{
    class ExcelEnginer : IDbEnginer
    {
        private readonly string TableNameMask = "TABLE_NAME";


        public ExcelEnginer()
        {
            _customHandler = new CoreE(CoreEA.CoreE.UsedDatabaseType.OleDb).X_Handler;
        }

        private Database _dbHandler = null;
        Database IDbEnginer.DbHandler
        {
            get
            {
                if (_dbHandler == null)
                {
                    throw new Exception("Handle has not created,please open database first");

                }
                return _dbHandler;
            }
        }

        private List<string> _tableList = null;
        public List<string> TableList
        {
            get
            {
                return _tableList;
            }

        }

        /// <summary>
        /// This must implicity
        /// </summary>
        /// <returns></returns>
        public List<string> GetTableList()
        {
            List<string> result = null;

            try
            {
                DataTable table = _customHandler.GetSchemaTable();

                if (table != null)
                {
                    result = new List<string>();
                    foreach (DataRow dr in table.Rows)
                    {
                       result.Add(dr[TableNameMask].ToString());
                    }
                }

                _tableList = result;

            }
            catch (Exception ce)
            {
                ProcessException.DisplayErrors(ce);
                result = null;
            }
            return result;
        }


        ICoreEAHander _customHandler = null;

        ICoreEAHander IDbEnginer.CustomHandler
        {
            get
            {
                return _customHandler;

            }
        }

        #region IDbEnginer Members

        public bool OpenDatabase(string dbName, string pwd, OpenModeClass curOpenMode)
        {
            string connStr = DbConnectionString.Excel.GetOleDbConnectionString(dbName, true);
            try
            {
                //_dbHandler = new AccessDatabase(connStr);

                //CoreEA.LoginDbInfo info = new CoreEA.LoginDbInfo(dbName, "", "Admin", "");
                _customHandler.Open(connStr);

            }
            catch (Exception e)
            {
                Debug.Write(e);
            }
            return GetTableList() != null;
        }

        bool IDbEnginer.CreateDataBase(string dbName, string pwd, bool Encrypt)
        {
            throw new NotImplementedException();
        }

        bool IDbEnginer.CreateDataBase(string dbName)
        {
            throw new NotImplementedException();
        }

        bool IDbEnginer.CreateDataBase(string dbName, string pwd)
        {
            throw new NotImplementedException();
        }

        bool IDbEnginer.OpenDatabase(string dbName, string pwd)
        {
            throw new NotImplementedException();
        }

        bool IDbEnginer.OpenDatabase(string dbName)
        {
            string connStr = DbConnectionString.Excel.GetOleDbConnectionString(dbName, true);
            try
            {
                //_dbHandler = new AccessDatabase(connStr);

                //CoreEA.LoginDbInfo info = new CoreEA.LoginDbInfo(dbName, "", "Admin", "");
                _customHandler.Open(connStr);

            }
            catch (Exception e)
            {
                throw e;
            }
            return GetTableList() != null;
        }

        bool IDbEnginer.CreateTableWithSchemaInfo(object args, string tableName)
        {
            throw new NotImplementedException();
        }


        System.Data.DataSet IDbEnginer.GetColumnInfoFromTable(string tableName)
        {
            throw new NotImplementedException();
        }

        System.Data.DataSet IDbEnginer.GetIndexInfoFromTable(string tableName)
        {
            throw new NotImplementedException();
        }

        System.Data.DataSet IDbEnginer.GetKeyColumnInfoFromTable(string tableName)
        {
            throw new NotImplementedException();
        }

        System.Data.DataSet IDbEnginer.GetProviderInfoFromTable(string tableName)
        {
            throw new NotImplementedException();
        }

        System.Data.DataSet IDbEnginer.GetSupportedDbType()
        {
            throw new NotImplementedException();
        }

        List<string> IDbEnginer.GetColumnNameListFromTable(string tableName)
        {
            throw new NotImplementedException();
        }

        List<string> IDbEnginer.GetIndexNameListFromTable(string tableName)
        {
            throw new NotImplementedException();
        }

        bool IDbEnginer.InsertData(string tableName, List<string> columnName, List<string> columnValue, List<System.Data.DbType> types)
        {
            throw new NotImplementedException();
        }

        int IDbEnginer.GetTableMaxLength
        {
            get { throw new NotImplementedException(); }
        }

        bool IDbEnginer.ExecuteSimpleCommand(string cmd)
        {
            throw new NotImplementedException();
        }


        #endregion

        #region IDisposable Members

        void IDisposable.Dispose()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
